package api

import (
	"fmt"
	"gateway/basic/config"
	__ "gateway/basic/proto/userproto"
	"gateway/handler/request"
	"gateway/middleware"
	"github.com/dgrijalva/jwt-go"
	"github.com/gin-gonic/gin"
	"net/http"
	"time"
)

func SendSms(c *gin.Context) {
	var req request.Code
	if err := c.ShouldBind(&req); err != nil {
		c.JSON(http.StatusBadRequest, gin.H{
			"code":    http.StatusBadRequest,
			"message": "请求参数错误",
			"error":   err.Error(),
		})
		return
	}
	_, err := config.UserClient.SendSms(c, &__.SendSmsReq{Mobile: req.Mobile})
	if err != nil {
		c.JSON(http.StatusInternalServerError, gin.H{
			"code":    http.StatusInternalServerError,
			"message": "发送短信失败",
			"error":   err.Error(),
		})
		return
	}
	c.JSON(http.StatusOK, gin.H{
		"code":    http.StatusOK,
		"message": "发送短信成功",
	})
}

func UserLogin(c *gin.Context) {
	var req request.SmsLogin
	if err := c.ShouldBind(&req); err != nil {
		c.JSON(http.StatusBadRequest, gin.H{
			"code":    http.StatusBadRequest,
			"message": "请求参数错误",
			"error":   err.Error(),
		})
		return
	}
	userId, err := config.UserClient.SmsLogin(c, &__.SmsLoginReq{
		Mobile: req.Mobile,
		Code:   req.Code,
	})
	if err != nil {
		c.JSON(http.StatusInternalServerError, gin.H{
			"code":    http.StatusInternalServerError,
			"message": "登录失败",
			"error":   err.Error(),
		})
		return
	}
	JWTId, err := middleware.NewJWT(config.JWTKEY).CreateToken(middleware.CustomClaims{
		ID:             uint(userId.Id),
		StandardClaims: jwt.StandardClaims{ExpiresAt: time.Now().Unix() + 60*60},
	})
	if err != nil {
		c.JSON(http.StatusInternalServerError, gin.H{
			"code":    http.StatusInternalServerError,
			"message": "登录失败",
			"error":   err.Error(),
		})
		return
	}
	c.JSON(http.StatusOK, gin.H{
		"code":    http.StatusOK,
		"message": "登录成功",
		"token":   JWTId,
	})
}

func PasswordLogin(c *gin.Context) {
	var req request.PasswordLogin
	if err := c.ShouldBind(&req); err != nil {
		c.JSON(http.StatusBadRequest, gin.H{
			"code":    http.StatusBadRequest,
			"message": "请求参数错误",
			"error":   err.Error(),
		})
		return
	}
	login, err := config.UserClient.PasswordLogin(c, &__.PasswordLoginReq{
		Mobile:   req.Mobile,
		Password: req.Password,
	})
	if err != nil {
		c.JSON(http.StatusInternalServerError, gin.H{
			"code":    http.StatusInternalServerError,
			"message": "登录失败",
			"error":   err.Error(),
		})
		return
	}
	fmt.Println("passwordLogin_id", login.Id)
	JWTId, err := middleware.NewJWT(config.JWTKEY).CreateToken(middleware.CustomClaims{
		ID:             uint(login.Id),
		StandardClaims: jwt.StandardClaims{ExpiresAt: time.Now().Unix() + 60*60},
	})
	if err != nil {
		c.JSON(http.StatusInternalServerError, gin.H{
			"code":    http.StatusInternalServerError,
			"message": "登录失败",
			"error":   err.Error(),
		})
		return
	}
	c.JSON(http.StatusOK, gin.H{
		"code":    http.StatusOK,
		"message": "登录成功",
		"token":   JWTId,
	})

}

func GetUserProfile(c *gin.Context) {
	var req request.GetUserProfileListReq
	if err := c.ShouldBind(&req); err != nil {
		c.JSON(http.StatusBadRequest, gin.H{
			"code":    http.StatusBadRequest,
			"message": "请求参数错误",
			"error":   err.Error(),
		})
		return
	}
	profile, err := config.UserClient.GetUserProfileList(c, &__.GetUserProfileReq{
		Page:     req.Page,
		PageSize: req.PageSize,
	})
	if err != nil {
		c.JSON(http.StatusInternalServerError, gin.H{
			"code":    http.StatusInternalServerError,
			"message": "获取用户信息失败",
			"error":   err.Error(),
		})
		return
	}
	c.JSON(http.StatusOK, gin.H{
		"code":    http.StatusOK,
		"message": "获取用户信息成功",
		"data":    profile,
	})
}

func UserProfile(c *gin.Context) {
	var req request.UserProfile
	if err := c.ShouldBind(&req); err != nil {
		c.JSON(http.StatusBadRequest, gin.H{
			"code":    http.StatusBadRequest,
			"message": "请求参数错误",
			"error":   err.Error(),
		})
		return
	}
	userId, _ := c.Get("userId")
	profile, err := config.UserClient.UserProfile(c, &__.UserProfileReq{
		Id:        int64(userId.(uint)),
		Password:  req.Password,
		NickName:  req.NickName,
		Signature: req.Signature,
		Sex:       req.Sex,
		Address:   req.Address,
	})
	if err != nil {
		c.JSON(http.StatusInternalServerError, gin.H{
			"code":    http.StatusInternalServerError,
			"message": "更新用户信息失败",
			"error":   err.Error(),
		})
		return
	}
	c.JSON(http.StatusOK, gin.H{
		"code":    http.StatusOK,
		"message": "更新用户信息成功",
		"data":    profile.Id,
	})
}

func GetUserDetail(c *gin.Context) {
	userId, _ := c.Get("userId")
	profile, err := config.UserClient.GetUserDetail(c, &__.GetUserDetailReq{
		Id: int64(userId.(uint)),
	})
	if err != nil {
		c.JSON(http.StatusInternalServerError, gin.H{
			"code":    http.StatusInternalServerError,
			"message": "获取用户详情失败",
			"error":   err.Error(),
		})
		return
	}
	c.JSON(http.StatusOK, gin.H{
		"code":    http.StatusOK,
		"message": "获取用户详情成功",
		"data":    profile,
	})

}
